﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using ESRI.ArcGIS.Geometry;
//using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SpatialAnalyst;
using ESRI.ArcGIS.GeoAnalyst;
using ESRI.ArcGIS.Display;
using System.Xml;
using ESRI.ArcGIS.DataSourcesFile;
using System.Data.OracleClient;


/// <summary>
/// IndexCalculatorInEngine 的摘要说明
/// </summary>
public class IndexCalculatorInEngine
{
    AoLicenseManager pAoLicenseManager = new AoLicenseManager();
	public IndexCalculatorInEngine()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
        pAoLicenseManager.Initialize();
	}

    ~IndexCalculatorInEngine()
    {
        pAoLicenseManager.Shutdown();
    }

    /// <summary>
    /// 计算样点光谱指数
    /// </summary>
    /// <param name="gpsSampleShapeFileName">样点矢量文件名</param>
    /// <param name="RasterDSName">多波段影像文件名</param>
    /// <param name="outFilePath">输出XML文档的路径</param>
    /// <param name="Blocksize">计算指数的像元块的大小，分1×1 3×3 5×5三种大小</param>
    /// <param name="orclConnStr">Oracle数据库连接字符串</param>
    /// <param name="sServer">SDE数据库IP地址</param>
    /// <param name="sInstance">SDE数据库实例名</param>
    /// <param name="sUser">SDE数据库用户名</param>
    /// <param name="sPasswd">SDE数据库用户密码</param>
    /// <param name="rootid">行政区</param>
    /// <param name="yearmonthday"></param>
    /// <returns></returns>
    public XmlDocument CaculateIndexToXmlDoc(string gpsSampleShapeFileName, string RasterDSName, string outFilePath, int Blocksize, string rootid, string yearmonthday) // String
    {

        IPointCollection pProjpoint;
        IWorkspace pWorkspace;
        IFeatureWorkspace pFeatWorkspace;
        IRasterWorkspaceEx pRasterWorkspace;
        IRasterDataset2 pRasterDS;
        IRaster pRaster;

        try
        {

            pProjpoint = new Multipoint();
            pWorkspace = Workspace.getSdeWorkspace();

            if (pWorkspace == null)
            {
                return null;
            }

            pFeatWorkspace = (IFeatureWorkspace)pWorkspace; 
            pProjpoint = ReadSamplesFromSDECreatMultipoint(pFeatWorkspace, gpsSampleShapeFileName);

            int pRowCount = 0;

            string strDate = yearmonthday.Split(' ')[0];
            string sqlString = "";
            if (Convert.ToInt16(rootid) < 50)
            {
                sqlString = "select count(*) from sp_rsindex where rootid=\'" + rootid + "\' and SAMPDATE=to_date(\'" + strDate + "\',\'yyyy-mm-dd\')";
            }
            else
            {

                sqlString = "select count(*) from sp_rsindex where distid=\'" + rootid + "\' and SAMPDATE=to_date(\'" + strDate + "\',\'yyyy-mm-dd\')";
            }

            pRowCount = Convert.ToInt32(AccessDatabase.Query(sqlString).Tables[0].Rows[0][0].ToString());


            if (pRowCount != 0) 
            {

                if (Convert.ToInt16(rootid) < 50)
                {
                    sqlString = "delete from sp_rsindex where rootid=\'" + rootid + "\' and SAMPDATE=to_date(\'" + strDate + "\',\'yyyy-mm-dd\')";
                }
                else
                {
                    sqlString = "delete from sp_rsindex where distid=\'" + rootid + "\' and SAMPDATE=to_date(\'" + strDate + "\',\'yyyy-mm-dd\')";
                }
                pRowCount = AccessDatabase.ExecuteSql(sqlString);

            }

            pRasterWorkspace = (IRasterWorkspaceEx)pWorkspace; 
            
            pRasterDS = (IRasterDataset2)pRasterWorkspace.OpenRasterDataset(RasterDSName);
            
            pRaster = pRasterDS.CreateFullRaster();

            XmlDocument xmlResult = new XmlDocument();
           
            xmlResult = MultiPointnnAverageIndexSet(gpsSampleShapeFileName, pFeatWorkspace, pProjpoint, pRaster, Blocksize, outFilePath, rootid, strDate);


            return xmlResult; // resultFileLocation

        }
        catch (Exception ex)
        {
            Log.WriteException(ex.Message.ToString());
            return null;

        }


    }



    private XmlDocument MultiPointnnAverageIndexSet(string gpsSampleShapeFileName, IFeatureWorkspace pFeatWork, IPointCollection pMultiPoint, IRaster pRaster, int n, string outFilePath, string rootid, string yearmonthday) // String
    {
        /*
         * --------------------------------------------------------------
        函数功能 求n*n邻域的平均遥感指数
        输入： 
        n是指以取样点（x_map,x_may）为中心的n*n的像素方阵
        输出：
        --------------------------------------------------------------
        */

        IRasterBandCollection pBandCol = (IRasterBandCollection)pRaster;
        IRaster2 pRaster2 = (IRaster2)pRaster;



        try
        {


            int Bandcount = pBandCol.Count;
            int i = 0;
            int j = 0;
            int k = 0;
            int l = 0;
            int col = 0;
            int row = 0;
            double[] pValue = new double[Bandcount - 1 + 1];
            //
            int pointcount = 0;
            pointcount = pMultiPoint.PointCount;
            //
            double[,] pIndex = new double[pointcount - 1 + 1, 8];
            for (i = 0; i <= pointcount - 1; i++)
            {
                for (j = 0; j <= 7; j++)
                {

                    pIndex[i, j] = 0;
                }

            }
            
            for (i = 0; i <= pointcount - 1; i++)
            {

                col = pRaster2.ToPixelColumn(pMultiPoint.get_Point(i).X);

                row = pRaster2.ToPixelRow(pMultiPoint.get_Point(i).Y);

                for (k = (col - (n - 1) / 2); k <= (col + (n - 1) / 2); k++)
                {

                    for (l = (row - (n - 1) / 2); l <= (row + (n - 1) / 2); l++)
                    {
                        for (j = 0; j <= Bandcount - 1; j++)
                        {

                            pValue[j] = Convert.ToDouble(pRaster2.GetPixelValue(j, k, l));
                        }

                        pIndex[i, 0] = pIndex[i, 0] + (pValue[3] - pValue[4]) / (pValue[3] + pValue[4]);
                        pIndex[i, 1] = pIndex[i, 1] + (pValue[3] - pValue[5]) / (pValue[3] + pValue[5]);
                        pIndex[i, 2] = pIndex[i, 2] + (pValue[3] - pValue[2]) / (pValue[3] + pValue[2]);
                        pIndex[i, 3] = pIndex[i, 3] + (pValue[1] - pValue[2]) / (pValue[1] + pValue[2]);
                        pIndex[i, 4] = pIndex[i, 4] + (pValue[3] - pValue[1]) / (pValue[3] + pValue[1]);
                        pIndex[i, 5] = pIndex[i, 5] + (pValue[1] - pValue[4]) / (pValue[1] + pValue[4]);
                        pIndex[i, 6] = pIndex[i, 6] + (pValue[1] - pValue[4]) / (pValue[1] + pValue[4]);
                        pIndex[i, 7] = pIndex[i, 7] + (pValue[1] - pValue[5]) / (pValue[1] + pValue[5]);


                    }

                }

                for (j = 0; j <= 7; j++)
                {
                    pIndex[i, j] = pIndex[i, j] / (n * n);
                }

            }



            XmlDocument xmldoc = new XmlDocument();
            XmlElement resamples;
            XmlElement[] point = new XmlElement[pointcount - 1 + 1];
            IFeatureClass pFeatClass;
            IFeature pFeat;
            IPoint xypoint = new Point();
            ITable agriTable;
            ICursor pCusor;
            IRow pRow;



            pFeatClass = pFeatWork.OpenFeatureClass(gpsSampleShapeFileName);
            IFeatureCursor pFeatCusor = pFeatClass.Search(null, false);

            pFeat = pFeatCusor.NextFeature();

            XmlElement name;
            XmlElement lon;
            XmlElement lat;
            XmlElement xx;
            XmlElement yy;
            XmlElement xmlRSI1;
            XmlElement xmlRSI2;
            XmlElement xmlRSI3;
            XmlElement xmlRSI4;
            XmlElement xmlRSI5;
            XmlElement xmlRSI6;
            XmlElement xmlRSI7;
            XmlElement xmlRSI8;
            XmlElement PlantGDW;
            XmlElement LAI;
            XmlElement SLFW;
            XmlElement MCL;

            resamples = xmldoc.CreateElement("resamples");
            string[] idlist = new string[pointcount - 1 + 1];


            //
            if (Convert.ToInt16(rootid) < 50)
            {
                if (pFeat.get_Value(pFeat.Fields.FindField("rootid")).ToString() != rootid)
                {
                    return null;
                }
            }
            else
            {
                if (pFeat.get_Value(pFeat.Fields.FindField("distid")).ToString() != rootid)
                {
                    return null;
                }
            }


            int pRowCount;
            string strSql = "";

            for (i = 0; i <= pointcount - 1; i++)
            {

                strSql = "insert into sp_rsindex values('"
                    + pFeat.get_Value(pFeat.Fields.FindField("pointid")).ToString() + "',"
                     + "''" + ","
                    + "to_date('" + yearmonthday + "','yyyy-mm-dd')" + ",'"
                    + pFeat.get_Value(pFeat.Fields.FindField("distid")).ToString() + "',"
                    + "''" + ","
                    + (Math.Round(pIndex[i, 0], 2)).ToString() + ","
                    + (Math.Round(pIndex[i, 1], 2)).ToString() + ","
                    + (Math.Round(pIndex[i, 2], 2)).ToString() + ","
                    + (Math.Round(pIndex[i, 3], 2)).ToString() + ","
                    + (Math.Round(pIndex[i, 4], 2)).ToString() + ","
                    + (Math.Round(pIndex[i, 5], 2)).ToString() + ","
                    + (Math.Round(pIndex[i, 6], 4)).ToString() + ","
                    + (Math.Round(pIndex[i, 7], 4)).ToString() + ",\'"
                    + pFeat.get_Value(pFeat.Fields.FindField("rootid")).ToString() + "\',"
                    + "0.0000" + ")";

                pRowCount = AccessDatabase.ExecuteSql(strSql);// cmd.ExecuteNonQuery();


                point[i] = xmldoc.CreateElement("Parcel");



                name = xmldoc.CreateElement("pointid");
                name.InnerText = pFeat.get_Value(pFeat.Fields.FindField("pointid")).ToString();
                point[i].AppendChild(name);
                idlist[i] = pFeat.get_Value(pFeat.Fields.FindField("pointid")).ToString();

                lon = xmldoc.CreateElement("Lon");
                lon.InnerText = pFeat.get_Value(pFeat.Fields.FindField("jin")).ToString();
                point[i].AppendChild(lon);

                lat = xmldoc.CreateElement("Lat");
                lat.InnerText = pFeat.get_Value(pFeat.Fields.FindField("wei")).ToString();
                point[i].AppendChild(lat);

                xx = xmldoc.CreateElement("X");
                xypoint = (IPoint)pFeat.Shape;
                xx.InnerText = xypoint.X.ToString();
                point[i].AppendChild(xx);

                yy = xmldoc.CreateElement("Y");
                xypoint = (IPoint)pFeat.Shape;
                yy.InnerText = xypoint.Y.ToString();
                point[i].AppendChild(yy);


                xmlRSI1 = xmldoc.CreateElement("NDWI");
                xmlRSI1.InnerText = (Math.Round(pIndex[i, 0], 2)).ToString();
                point[i].AppendChild(xmlRSI1);

                xmlRSI2 = xmldoc.CreateElement("NDWI2");
                xmlRSI2.InnerText = (Math.Round(pIndex[i, 1], 2)).ToString();
                point[i].AppendChild(xmlRSI2);

                xmlRSI3 = xmldoc.CreateElement("NDVI");
                xmlRSI3.InnerText = (Math.Round(pIndex[i, 2], 2)).ToString();
                point[i].AppendChild(xmlRSI3);

                xmlRSI4 = xmldoc.CreateElement("NRI");
                xmlRSI4.InnerText = (Math.Round(pIndex[i, 3], 2)).ToString();
                point[i].AppendChild(xmlRSI4);

                xmlRSI5 = xmldoc.CreateElement("NSI");
                xmlRSI5.InnerText = (Math.Round(pIndex[i, 4], 2)).ToString();
                point[i].AppendChild(xmlRSI5);

                xmlRSI6 = xmldoc.CreateElement("WI");
                xmlRSI6.InnerText = (Math.Round(pIndex[i, 5], 2)).ToString();
                point[i].AppendChild(xmlRSI6);

                xmlRSI7 = xmldoc.CreateElement("G_SWIR");
                xmlRSI7.InnerText = (Math.Round(pIndex[i, 6], 4)).ToString();
                point[i].AppendChild(xmlRSI7);

                xmlRSI8 = xmldoc.CreateElement("G_SWIR2");
                xmlRSI8.InnerText = (Math.Round(pIndex[i, 7], 4)).ToString();
                point[i].AppendChild(xmlRSI8);

                resamples.AppendChild(point[i]);
                pFeat = pFeatCusor.NextFeature();


            }


            xmldoc.AppendChild(resamples);

            agriTable = pFeatWork.OpenTable("SP_AGRI_PARAM");

            XmlNodeList xmlNlist;
            XmlNode xmlNode;
            xmlNlist = xmldoc.GetElementsByTagName("Parcel");
            IQueryFilter pQueryFilter;
            pQueryFilter = new QueryFilter();
            pQueryFilter.SubFields = "LAI,MCL,SLFW,PLANTGDW";

            if (Convert.ToInt16(rootid) < 50)
            {

                for (i = 0; i <= pointcount - 1; i++)
                {


                    pQueryFilter.WhereClause = "rootid=\'" + rootid + "\' and SAMPDATE = to_date(\'" + yearmonthday + "\',\'yyyy-mm-dd\')" + " and (POINTID = \'" + idlist[i] + "\')";

                    pCusor = agriTable.Search(pQueryFilter, false);

                    pRow = pCusor.NextRow();

                    xmlNode = xmlNlist.Item(i);
                    //

                    PlantGDW = xmldoc.CreateElement("PlantGDW");
                    PlantGDW.InnerText = pRow.get_Value(pRow.Fields.FindField("PlantGDW")).ToString();
                    xmlNode.AppendChild(PlantGDW);

                    LAI = xmldoc.CreateElement("LAI");
                    LAI.InnerText = pRow.get_Value(pRow.Fields.FindField("LAI")).ToString();
                    xmlNode.AppendChild(LAI);

                    SLFW = xmldoc.CreateElement("SLFW");
                    SLFW.InnerText = pRow.get_Value(pRow.Fields.FindField("SLFW")).ToString();
                    xmlNode.AppendChild(SLFW);

                    MCL = xmldoc.CreateElement("MCL");
                    MCL.InnerText = pRow.get_Value(pRow.Fields.FindField("MCL")).ToString();
                    xmlNode.AppendChild(MCL);


                }
            }
            else
            {
                for (i = 0; i <= pointcount - 1; i++)
                {


                    pQueryFilter.WhereClause = "distid=\'" + rootid + "\' and SAMPDATE = to_date(\'" + yearmonthday + "\',\'yyyy-mm-dd\')" + " and (POINTID = \'" + idlist[i] + "\')";

                    pCusor = agriTable.Search(pQueryFilter, false);

                    pRow = pCusor.NextRow();

                    xmlNode = xmlNlist.Item(i);
                    //

                    PlantGDW = xmldoc.CreateElement("PlantGDW");
                    PlantGDW.InnerText = pRow.get_Value(pRow.Fields.FindField("PlantGDW")).ToString();
                    xmlNode.AppendChild(PlantGDW);

                    LAI = xmldoc.CreateElement("LAI");
                    LAI.InnerText = pRow.get_Value(pRow.Fields.FindField("LAI")).ToString();
                    xmlNode.AppendChild(LAI);

                    SLFW = xmldoc.CreateElement("SLFW");
                    SLFW.InnerText = pRow.get_Value(pRow.Fields.FindField("SLFW")).ToString();
                    xmlNode.AppendChild(SLFW);

                    MCL = xmldoc.CreateElement("MCL");
                    MCL.InnerText = pRow.get_Value(pRow.Fields.FindField("MCL")).ToString();
                    xmlNode.AppendChild(MCL);


                }

            }


            //Dim strFileLocation As String = outFilePath
            //Dim timeNow As Date = Date.Now
            //Dim timeString As String = timeNow.Hour.ToString + _
            //timeNow.Minute.ToString + timeNow.Second.ToString + timeNow.Millisecond.ToString



            //strFileLocation += timeString
            //strFileLocation += ".xml"

            //xmldoc.Save(strFileLocation)
            //conn.Close(); //

            pBandCol = null;
            pRaster2 = null;
            i = 0;
            j = 0;
            row = 0;
            col = 0;
            pointcount = 0;
            pValue = null;
            pIndex = null;
            pFeatClass = null;
            pFeat = null;
            pFeatCusor = null;

            //Return strFileLocation
            return xmldoc;


        }
        catch (Exception ex)
        {
            Log.WriteException(ex.Message.ToString());
            return null; //  "Error" + ex.ToString

        }


    }



    //==========================================================================
    //==========================================================================
    //==========================================================================
    //==========================================================================
    //==========================================================================


        public MultipointClass ReadSamplesFromSDECreatMultipoint(IFeatureWorkspace shapefileWS, string shapefilename)
        {

            //IMultipoint pMultiPoint = new MultipointClass();// default(Multipoint);
            //IFeatureClass pFeatClass = new FeatureClassClass();// default(IFeatureClass);
            IPointCollection pPointCollection =new MultipointClass();
            double x = 0.0;
            double y = 0.0;
             //IFeature pFeat = new FeatureClass();// default(IFeature);
           
            
            try {


                IFeatureClass pFeatClass = shapefileWS.OpenFeatureClass(shapefilename);
                //打开shapefile
                IFeatureCursor pFeatCusor = pFeatClass.Search(null, true);
                //得到shapefile中的图元指针
                IFeature pFeat = pFeatCusor.NextFeature();
                //得到首图元

                object missing = Type.Missing;
                while ((pFeat != null)) {
                    
                    IPoint point = new Point();
                    IPoint point1 = new Point();
                    point = (IPoint)pFeat.Shape;
                    //得到投影后的空间数据
                    
                    //得到投影坐标
                    x = point.X;
                    y = point.Y;
                    
                    //投影坐标加入pMultipoint中
                    point1.PutCoords(x, y);
                    pPointCollection.AddPoint(point1, ref missing, ref missing);
                    pFeat = pFeatCusor.NextFeature();
                    point = null;
                        
                    point1 = null;
                }

                return (MultipointClass)pPointCollection;
            }
            catch (Exception ex) {

                Log.WriteException(ex.Message.ToString());
                return null;
            }
        }




    //public XmlDocument GetIndexDataSet(string gpsSampleShapeFileName, string RasterDSName, string outFilePath, int Blocksize, string rootid, string yearmonthday)
    public XmlDocument GetIndexDataSet(string gpsSampleShapeFileName, string RasterDSName, string outFilePath, int Blocksize, string rootid, string yearmonthday)
    {
        //----------------------------------------------------
        //函数功能：打开工作空间，求解病虫害遥感指数
        //输入：
        //输出：

        //
        //----------------------------------------------------
        //IPointCollection pProjpoint = default(IPointCollection);
        //IWorkspace pWorkspace = default(IWorkspace);
        ////OpenWorkspace openWP = new OpenWorkspace();
        //IFeatureWorkspace pFeatWorkspace = default(IFeatureWorkspace);
        ////定义一个用于打开shape文件或属性表的工作空间
        //IRasterWorkspaceEx pRasterWorkspace = default(IRasterWorkspaceEx);
        ////定义一个用于打开SdeRaster的工作空间的对象
        //IRasterDataset2 pRasterDS = default(IRasterDataset2);
        ////定义一个栅格数据集对象
        //IRaster pRaster = default(IRaster);
        ////定义一个临时栅格对象

        try
        {
            //pProjpoint = new Multipoint();
            IWorkspace pWorkspace = Workspace.getSdeWorkspace();

            IFeatureWorkspace pFeatWorkspace = (IFeatureWorkspace)pWorkspace;
            //将一般的工作空间实例化到图元工作空间
            IPointCollection pProjpoint = (MultipointClass)ReadSamplesFromSDECreatMultipoint(pFeatWorkspace, gpsSampleShapeFileName);

            IRasterWorkspaceEx pRasterWorkspace = (IRasterWorkspaceEx)pWorkspace;
            //将一般的工作空间实例化到raster工作空间

            //打开一个栅格数据集和赋给栅格对象
            IRasterDataset2 pRasterDS = (IRasterDataset2)pRasterWorkspace.OpenRasterDataset(RasterDSName);


            IRaster pRaster = pRasterDS.CreateFullRaster();




            //接收外部输入，计算遥感指数集，返回结果
            int pRowCount = 0;
            string strSql = "";
            string strDate = yearmonthday.Split(' ')[0];
            //cmd.Connection = conn;
            strSql = "select count(*) from sp_hyperindex where SAMPDATE=to_date('" + strDate + "','yyyy-mm-dd')";
            pRowCount = Convert.ToInt32(AccessDatabase.Query(strSql).Tables[0].Rows[0][0].ToString());

            if (pRowCount != 0) //ljy090304
            {

                if (Convert.ToInt16(rootid) < 50)
                {
                    strSql = "delete from sp_hyperindex where rootid=\'" + rootid + "\' and SAMPDATE=to_date(\'" + strDate + "\',\'yyyy-mm-dd\')";

                }
                else
                {
                    strSql = "delete from sp_hyperindex where distid=\'" + rootid + "\' and SAMPDATE=to_date(\'" + strDate + "\',\'yyyy-mm-dd\')";
                }
                pRowCount = AccessDatabase.ExecuteSql(strSql);

            }


            strSql = "delete from sp_hyperindex where SAMPDATE=to_date('" + strDate + "','yyyy-mm-dd')";
            pRowCount = AccessDatabase.ExecuteSql(strSql);
            //XmlDocument resultFile = MultiPointnnAverageIndexSetForDisease(gpsSampleShapeFileName, pFeatWorkspace, pProjpoint, pRaster, Blocksize, outFilePath, strDate);
            XmlDocument resultFile = MultiPointnnAverageIndexSetForDisease(gpsSampleShapeFileName, pFeatWorkspace, pProjpoint, pRaster, Blocksize, outFilePath, rootid,strDate);


            //pRasterWorkspace = null;
            //pWorkspace = null;
            //pRasterDS = null;
            //pRaster = null;

            return resultFile;
        }
        catch (Exception ex)
        {
            Log.WriteException(ex.Message.ToString());
            return null;


        }
    }


    private XmlDocument MultiPointnnAverageIndexSetForDisease(string gpsSampleShapeFileName, IFeatureWorkspace pFeatWork, IPointCollection pMultiPoint, IRaster pRaster, int n, string outFilePath, string rootid, string yearmonthday)
        {
            
            //--------------------------------------------------------------
            //函数功能 求n*n邻域的平均遥感指数
            //输入： 
            //n是指以取样点（x_map,x_may）为中心的n*n的像素方阵
            //输出：
            //
            //
            
            //--------------------------------------------------------------
            
            IRasterBandCollection pBandCol = (IRasterBandCollection)pRaster;
            //pBandCol = pRaster;
            IRaster2 pRaster2 = (IRaster2)pRaster;
            //pRaster2 = pRaster;
            
            
            
            try {
                
                
                int Bandcount = pBandCol.Count;
                int i = 0;
                int j = 0;
                int k = 0;
                int l = 0;
                int col = 0;
                int row = 0;
                double[] pValue = new double[Bandcount];
                //求采样点个数
                int pointcount = 0;
                pointcount = pMultiPoint.PointCount;
                //定义一个存储最终结果的二维数组
                double[,] pIndex = new double[pointcount, 14];
                for (i = 0; i <= pointcount - 1; i++) {
                    for (j = 0; j <= 13; j++) {
                        
                        pIndex[i, j] = 0;
                        
                    }
                }
                
                //逐采样点求对应的n*n块的遥感指数
                for (i = 0; i <= pointcount - 1; i++) {
                    
                    //由平面坐标求像素的行列值
                    col = pRaster2.ToPixelColumn(pMultiPoint.get_Point(i).X);

                    row = pRaster2.ToPixelRow(pMultiPoint.get_Point(i).Y);
                    
                    for (k = (col - (n - 1) / 2); k <= (col + (n - 1) / 2); k++) {
                        
                        for (l = (row - (n - 1) / 2); l <= (row + (n - 1) / 2); l++) {
                            //求各波段的像素值
                            for (j = 0; j <= Bandcount - 1; j++) {

                                pValue[j] = Convert.ToDouble(pRaster2.GetPixelValue(j, k, l));
                            }
                            
                            
                            
                            //求某样点的14个遥感指数：NDWI, TVI, NDVI, NSI, WI, NDGI, GEMI，EVI,RVI,ARVI,SAVI,MSAVI,DVI,RDVI并存储到二维数组
                            
                            
                            
                            if (Bandcount == 7 | Bandcount == 8) //根据影像的波段数分别计算指数
                            {
                                pIndex[i, 0] = pIndex[i, 0] + (pValue[3] - pValue[4]) / (pValue[3] + pValue[4]);  //NDWI
                                pIndex[i, 1] = pIndex[i, 1] + 60 * pValue[3] - 100 * pValue[2] + 40 * pValue[1];  //TVI  
                                pIndex[i, 2] = pIndex[i, 2] + (pValue[3] - pValue[2]) / (pValue[3] + pValue[2]);  //NDVI
                                pIndex[i, 3] = pIndex[i, 3] + (pValue[3] - pValue[1]) / (pValue[3] + pValue[1]);  //NSI
                                pIndex[i, 4] = pIndex[i, 4] + (pValue[6] - pValue[4]) / (pValue[6] + pValue[4]);  //WI
                                pIndex[i, 5] = pIndex[i, 5] + (pValue[1] - pValue[2]) / (pValue[1] + pValue[2]);  //NDGI
                                pIndex[i, 6] = pIndex[i, 6] + (2 * (pValue[3] * pValue[3] - pValue[2] * pValue[2]) + 1.5 * pValue[3] + 0.5 * pValue[2]) / (pValue[3] + pValue[2] + 0.5) * (1 - 0.25 * (2 * (pValue[3] * pValue[3] - pValue[2] * pValue[2]) + 1.5 * pValue[3] + 0.5 * pValue[2]) / (pValue[3] + pValue[2] + 0.5)) - (pValue[2] - 0.125) / (1 - pValue[2]);  //GEMI
                                pIndex[i, 7] = pIndex[i, 7] + 2 * (pValue[3] - pValue[2]) / (pValue[3] + 6 * pValue[2] - 7.5 * pValue[0] + 1);  //EVI
                                pIndex[i, 8] = pIndex[i, 8] + pValue[3] / pValue[2];  //RVI                                
                                pIndex[i, 9] = pIndex[i, 9] + (pValue[3] - (2 * pValue[2] - pValue[0])) / (pValue[3] + (2 * pValue[2] - pValue[0]));   //ARVI
                                pIndex[i, 10] = pIndex[i, 10] + 1.5 * (pValue[3] - pValue[2]) / (pValue[3] + pValue[2] + 0.5);  //SAVI
                                pIndex[i, 11] = pIndex[i, 11] + (2 * pValue[3] + 1) - Math.Sqrt(Math.Abs(Math.Pow((2 * pValue[3] + 1), 2) - 8 * (pValue[3] - pValue[2]))) / 2;  //MSAVI                              
                                pIndex[i, 12] = pIndex[i, 12] + (pValue[3] - pValue[2]);  //DVI
                            }
                            else if (Bandcount == 6)
                            {
                                pIndex[i, 0] = pIndex[i, 0] + (pValue[3] - pValue[4]) / (pValue[3] + pValue[4]);  //NDWI
                                pIndex[i, 1] = pIndex[i, 1] + 60 * pValue[3] - 100 * pValue[2] + 40 * pValue[1];  //TVI  
                                pIndex[i, 2] = pIndex[i, 2] + (pValue[3] - pValue[2]) / (pValue[3] + pValue[2]);  //NDVI
                                pIndex[i, 3] = pIndex[i, 3] + (pValue[3] - pValue[1]) / (pValue[3] + pValue[1]);  //NSI
                                pIndex[i, 4] = pIndex[i, 4] + (pValue[5] - pValue[4]) / (pValue[5] + pValue[4]);  //WI
                                pIndex[i, 5] = pIndex[i, 5] + (pValue[1] - pValue[2]) / (pValue[1] + pValue[2]);  //NDGI
                                pIndex[i, 6] = pIndex[i, 6] + (2 * (pValue[3] * pValue[3] - pValue[2] * pValue[2]) + 1.5 * pValue[3] + 0.5 * pValue[2]) / (pValue[3] + pValue[2] + 0.5) * (1 - 0.25 * (2 * (pValue[3] * pValue[3] - pValue[2] * pValue[2]) + 1.5 * pValue[3] + 0.5 * pValue[2]) / (pValue[3] + pValue[2] + 0.5))-(pValue[2]-0.125)/(1-pValue[2]);  //GEMI
                                pIndex[i, 7] = pIndex[i, 7] + 2 * (pValue[3] - pValue[2]) / (pValue[3] + 6 * pValue[2] - 7.5 * pValue[0] + 1);  //EVI
                                pIndex[i, 8] = pIndex[i, 8] + pValue[3]/pValue[2];  //RVI                                
                                pIndex[i, 9] = pIndex[i, 9] + (pValue[3] - (2 * pValue[2] - pValue[0])) / (pValue[3] + (2 * pValue[2] - pValue[0]));   //ARVI
                                pIndex[i, 10] = pIndex[i, 10] + 1.5 * (pValue[3] - pValue[2]) / (pValue[3] + pValue[2] + 0.5);  //SAVI
                                pIndex[i, 11] = pIndex[i, 11] + (2 * pValue[3] + 1) - Math.Sqrt(Math.Abs(Math.Pow((2 * pValue[3] + 1), 2) - 8 * (pValue[3] - pValue[2])))/2;  //MSAVI                              
                                pIndex[i, 12] = pIndex[i, 12] + (pValue[3] - pValue[2]);  //DVI
                            }
                            else if (Bandcount == 4) 
                            {
                                pIndex[i, 0] = pIndex[i, 0] + (pValue[2] - pValue[3]) / (pValue[2] + pValue[3]);  //NDWI
                                pIndex[i, 1] = pIndex[i, 1] + 60 * pValue[2] - 100 * pValue[1] + 40 * pValue[0];  //TVI
                                pIndex[i, 2] = pIndex[i, 2] + (pValue[2] - pValue[1]) / (pValue[2] + pValue[1]);  //NDVI
                                pIndex[i, 3] = pIndex[i, 3] + (pValue[2] - pValue[0]) / (pValue[2] + pValue[0]);  //NSI
                                pIndex[i, 4] = 0;  //WI
                                pIndex[i, 5] = pIndex[i, 5] + (pValue[0] - pValue[1]) / (pValue[0] + pValue[1]);  //NDGI
                                pIndex[i, 6] = pIndex[i, 6] + (2 * (pValue[2] * pValue[2] - pValue[1] * pValue[1]) + 1.5 * pValue[2] + 0.5 * pValue[1]) / (pValue[2] + pValue[1] + 0.5) * (1 - 0.25 * (2 * (pValue[2] * pValue[2] - pValue[1] * pValue[1]) + 1.5 * pValue[2] + 0.5 * pValue[1]) / (pValue[2] + pValue[1] + 0.5)) - (pValue[1] - 0.125) / (1 - pValue[1]);  //GEMI
                                pIndex[i, 7] = 0;  //EVI
                                pIndex[i, 8] = pIndex[i, 8] + pValue[2] / pValue[1];  //RVI
                                pIndex[i, 9] = 0;  //ARVI
                                pIndex[i, 10] = pIndex[i, 10] + 1.5 * (pValue[2] - pValue[1]) / (pValue[2] + pValue[1] + 0.5);   //SAVI                             
                                pIndex[i, 11] = pIndex[i, 11] + 0.5 * ((2 * pValue[2] + 1) - Math.Sqrt(Math.Abs(Math.Pow((2 * pValue[2] + 1), 2) - 8 * (pValue[2] - pValue[1]))));  //MSAVI                                  
                                pIndex[i, 12] = pIndex[i, 12] + (pValue[2] - pValue[1]);  //DVI
                            }
                            else if (Bandcount == 1)  //高光谱影像的红边位置影像
                            {
                                pIndex[i, 0] = pIndex[i, 0] + pValue[0];
                                pIndex[i, 1] = 0;
                                pIndex[i, 2] = 0;
                                pIndex[i, 3] = 0;
                                pIndex[i, 4] = 0;
                                pIndex[i, 5] = 0;
                                pIndex[i, 6] = 0;
                                pIndex[i, 7] = 0;
                                pIndex[i, 8] = 0;
                                pIndex[i, 9] = 0;
                                pIndex[i, 10] = 0;
                                pIndex[i, 11] = 0;
                                pIndex[i, 12] = 0;
                            }
                                
                            
                        }
                            
                        
                    }
                    
                    //求n*n遥感指数的平均值
                    for (j = 0; j <= 12; j++) {
                        pIndex[i, j] = pIndex[i, j] / (n * n);
                    }
                        
                    pIndex[i, 13] = Math.Sqrt(Math.Abs(pIndex[i, 2] * pIndex[i, 12]));   //RDVI
                }
                
                
                //输出一个XML文件
                
                
                
                XmlDocument xmldoc = new XmlDocument();
                XmlElement resamples = default(XmlElement);
                XmlElement[] point = new XmlElement[pointcount];
                IFeatureClass pFeatClass = default(IFeatureClass);
                IFeature pFeat = default(IFeature);
                IPoint xypoint = new Point();
                //ITable agriTable = default(ITable);
                /////''''''''''''''''''''''''''''''''
                //ICursor pCusor = default(ICursor);
                //IRow pRow = default(IRow);
                
                
                
                pFeatClass = pFeatWork.OpenFeatureClass(gpsSampleShapeFileName);
                IFeatureCursor pFeatCusor = pFeatClass.Search(null, false);
                
                pFeat = pFeatCusor.NextFeature();
                
                XmlElement ID = default(XmlElement);
                XmlElement name = default(XmlElement);
                XmlElement lon = default(XmlElement);
                XmlElement lat = default(XmlElement);
                XmlElement xx = default(XmlElement);
                XmlElement yy = default(XmlElement);
                XmlElement xmlRSI1 = default(XmlElement);
                XmlElement xmlRSI2 = default(XmlElement);
                XmlElement xmlRSI3 = default(XmlElement);
                //XmlElement xmlRSI4 = default(XmlElement);
                XmlElement xmlRSI5 = default(XmlElement);
                XmlElement xmlRSI6 = default(XmlElement);
                XmlElement xmlRSI7 = default(XmlElement);
                XmlElement xmlRSI8 = default(XmlElement);
                XmlElement xmlRSI9 = default(XmlElement);
                XmlElement xmlRSI10 = default(XmlElement);
                XmlElement xmlRSI11 = default(XmlElement);
                XmlElement xmlRSI12 = default(XmlElement);
                XmlElement xmlRSI13 = default(XmlElement);
                XmlElement xmlRSI14 = default(XmlElement);
                XmlElement xmlRSI15 = default(XmlElement);
                //Dim PlantGDW, LAI, SLFW, MCL As XmlElement
                //XmlElement DiseaseIndex = default(XmlElement);
                //XmlElement CHL_AB = default(XmlElement);
                //XmlElement LAI = default(XmlElement);
                //XmlElement MCL = default(XmlElement);
                
                resamples = xmldoc.CreateElement("resamples");
                string[] idlist = new string[pointcount];

                int pRowCount = 0;
                //cmd.Connection = conn;
                string strSql = "";
                
                
                for (i = 0; i <= pointcount - 1; i++) {

                    string str1 = pFeat.get_Value(pFeat.Fields.FindField("POINTID")).ToString();
                    string str2 = pFeat.get_Value(pFeat.Fields.FindField("POINTNAME")).ToString();
                    string str3 = pFeat.get_Value(pFeat.Fields.FindField("DISTID")).ToString();
                    string str4 = pFeat.get_Value(pFeat.Fields.FindField("ROOTID")).ToString();                    
                    
                    strSql = "insert into sp_hyperindex values('"
                        + pFeat.get_Value(pFeat.Fields.FindField("POINTID")).ToString() + "','"
                        + pFeat.get_Value(pFeat.Fields.FindField("POINTNAME")).ToString() + "',"
                        + "to_date('" + yearmonthday + "','yyyy-mm-dd')" + ",'"
                        + pFeat.get_Value(pFeat.Fields.FindField("DISTID")).ToString() + "',"
                        + "''" + ",'"
                        + pFeat.get_Value(pFeat.Fields.FindField("ROOTID")).ToString() + "',"
                        + "0.00" + ","
                        + "0.00" + ","
                        + "0.00" + ","
                        + "0.00" + ","
                        + Convert.ToString(Math.Round(pIndex[i, 0], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 1], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 2], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 3], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 4], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 5], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 6], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 7], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 8], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 9], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 10], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 11], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 12], 2)) + ","
                        + Convert.ToString(Math.Round(pIndex[i, 13], 2)) + ",'"
                        + gpsSampleShapeFileName + "')";

                    pRowCount = AccessDatabase.ExecuteSql(strSql);
                    //pRowCount =  oracle .ExecuteNonQuery();
                    //执行sql 并返回受影响的行数
                    
                    
                    point[i] = xmldoc.CreateElement("Parcel");
                    
                    
                    
                    ID = xmldoc.CreateElement("pointid");
                    ID.InnerText = pFeat.get_Value(pFeat.Fields.FindField("POINTID")).ToString();
                    point[i].AppendChild(ID);
                    idlist[i] = pFeat.get_Value(pFeat.Fields.FindField("POINTID")).ToString();
                    //为了和属性表对应
                    
                    name = xmldoc.CreateElement("pointName");
                    name.InnerText = pFeat.get_Value(pFeat.Fields.FindField("POINTNAME")).ToString();
                    point[i].AppendChild(name);
                    
                    lon = xmldoc.CreateElement("Lon");
                    lon.InnerText = pFeat.get_Value(pFeat.Fields.FindField("JIN")).ToString();
                    point[i].AppendChild(lon);
                    
                    lat = xmldoc.CreateElement("Lat");
                    lat.InnerText = pFeat.get_Value(pFeat.Fields.FindField("WEI")).ToString();
                    point[i].AppendChild(lat);
                    
                    xx = xmldoc.CreateElement("X");
                    xypoint = (IPoint)pFeat.Shape;
                    xx.InnerText = xypoint.X.ToString();
                    point[i].AppendChild(xx);
                    
                    yy = xmldoc.CreateElement("Y");
                    xypoint = (IPoint)pFeat.Shape;
                    yy.InnerText = xypoint.Y.ToString();
                    point[i].AppendChild(yy);
                    
                    
                    xmlRSI1 = xmldoc.CreateElement("NDWI");
                    xmlRSI1.InnerText = (Math.Round(pIndex[i, 0], 2)).ToString();
                    point[i].AppendChild(xmlRSI1);
                    
                    xmlRSI2 = xmldoc.CreateElement("TVI");
                    xmlRSI2.InnerText = (Math.Round(pIndex[i, 1], 2)).ToString();
                    point[i].AppendChild(xmlRSI2);
                    
                    xmlRSI3 = xmldoc.CreateElement("NDVI");
                    xmlRSI3.InnerText = (Math.Round(pIndex[i, 2], 2)).ToString();
                    point[i].AppendChild(xmlRSI3);
                    
                    //xmlRSI4 = xmldoc.CreateElement("NRI");
                    //xmlRSI4.InnerText = (Math.Round(pIndex[i, 3], 2)).ToString();
                    //point[i].AppendChild(xmlRSI4);
                    
                    xmlRSI5 = xmldoc.CreateElement("NSI");
                    xmlRSI5.InnerText = (Math.Round(pIndex[i, 3], 2)).ToString();
                    point[i].AppendChild(xmlRSI5);
                    
                    xmlRSI6 = xmldoc.CreateElement("WI");
                    xmlRSI6.InnerText = (Math.Round(pIndex[i, 4], 2)).ToString();
                    point[i].AppendChild(xmlRSI6);
                    
                    xmlRSI7 = xmldoc.CreateElement("NDGI");
                    xmlRSI7.InnerText = (Math.Round(pIndex[i, 5], 2)).ToString();
                    point[i].AppendChild(xmlRSI7);
                    
                    xmlRSI8 = xmldoc.CreateElement("GEMI");
                    xmlRSI8.InnerText = (Math.Round(pIndex[i, 6], 2)).ToString();
                    point[i].AppendChild(xmlRSI8);
                    
                    xmlRSI9 = xmldoc.CreateElement("EVI");
                    xmlRSI9.InnerText = (Math.Round(pIndex[i, 7], 2)).ToString();
                    point[i].AppendChild(xmlRSI9);
                    
                    xmlRSI10 = xmldoc.CreateElement("RVI");
                    xmlRSI10.InnerText = (Math.Round(pIndex[i, 8], 2)).ToString();
                    point[i].AppendChild(xmlRSI10);
                    
                    xmlRSI11 = xmldoc.CreateElement("ARVI");
                    xmlRSI11.InnerText = (Math.Round(pIndex[i, 9], 2)).ToString();
                    point[i].AppendChild(xmlRSI11);
                    
                    xmlRSI12 = xmldoc.CreateElement("SAVI");
                    xmlRSI12.InnerText = (Math.Round(pIndex[i, 10], 2)).ToString();
                    point[i].AppendChild(xmlRSI12);
                    
                    xmlRSI13 = xmldoc.CreateElement("MSAVI");
                    xmlRSI13.InnerText = (Math.Round(pIndex[i, 11], 2)).ToString();
                    point[i].AppendChild(xmlRSI13);
                    
                    xmlRSI14 = xmldoc.CreateElement("DVI");
                    xmlRSI14.InnerText = (Math.Round(pIndex[i, 12], 2)).ToString();
                    point[i].AppendChild(xmlRSI14);
                    
                    xmlRSI15 = xmldoc.CreateElement("RDVI");
                    xmlRSI15.InnerText = (Math.Round(pIndex[i, 13], 2)).ToString();
                    point[i].AppendChild(xmlRSI15);
                    
                    resamples.AppendChild(point[i]);
                        
                    pFeat = pFeatCusor.NextFeature();
                }
                
                                
                xmldoc.AppendChild(resamples);

                //if (gpsSampleShapeFileName.Contains("XJ"))
                //{
                //    agriTable = pFeatWork.OpenTable("RS.XJ_DISEASE_PARAM");
                //}
                //else
                //{
                //    agriTable = pFeatWork.OpenTable("RS.SP_DISEASE_PARAM");
                //}

                //XmlNodeList xmlNlist = default(XmlNodeList);
                //XmlNode xmlNode = default(XmlNode);
                //xmlNlist = xmldoc.GetElementsByTagName("Parcel");
                //IQueryFilter pQueryFilter = default(IQueryFilter);
                //pQueryFilter = new QueryFilter();

                //if (gpsSampleShapeFileName.Contains("XJ"))
                //{
                //    pQueryFilter.SubFields = "DISEASEINDEX,LAI,MCL,LEAFGDW";
                //}
                //else
                //{
                //    pQueryFilter.SubFields = "DISEASEINDEX,CHL_AB,LAI,MCL,LEAFGDW";
                //}
                
                
                //for (i = 0; i <= pointcount - 1; i++) {
                    
                //    pQueryFilter.WhereClause = "SAMPDATE = to_date('" + yearmonthday + "','yyyy-mm-dd')" + " and (POINTID = '" + idlist[i] + "')";
                    
                //    pCusor = agriTable.Search(pQueryFilter, false);
                    
                //    pRow = pCusor.NextRow();
                    
                //    xmlNode = xmlNlist.Item(i);
                //    // 
                    
                //    //PlantGDW = xmldoc.CreateElement("PlantGDW")
                //    //PlantGDW.InnerText = pRow.Value(pRow.Fields.FindField("PlantGDW"))
                //    //xmlNode.AppendChild(PlantGDW)
                    
                //    //LAI = xmldoc.CreateElement("LAI")
                //    //LAI.InnerText = pRow.Value(pRow.Fields.FindField("LAI"))
                //    //xmlNode.AppendChild(LAI)
                    
                //    //SLFW = xmldoc.CreateElement("SLFW")
                //    //SLFW.InnerText = pRow.Value(pRow.Fields.FindField("SLFW"))
                //    //xmlNode.AppendChild(SLFW)
                    
                //    //MCL = xmldoc.CreateElement("MCL")
                //    //MCL.InnerText = pRow.Value(pRow.Fields.FindField("MCL"))
                //    //xmlNode.AppendChild(MCL)
                //    // pQueryFilter.SubFields = "DISEASEINDEX,CHL_AB,LAI,MCL,LEAFGDW"
                //    DiseaseIndex = xmldoc.CreateElement("DiseaseIndex");
                //    DiseaseIndex.InnerText = pRow.get_Value(pRow.Fields.FindField("DISEASEINDEX")).ToString();
                //    xmlNode.AppendChild(DiseaseIndex);

                //    if (gpsSampleShapeFileName.Contains("HLJ"))
                //    {
                //        CHL_AB = xmldoc.CreateElement("CHL_AB");
                //        CHL_AB.InnerText = pRow.get_Value(pRow.Fields.FindField("CHL_AB")).ToString();
                //        xmlNode.AppendChild(CHL_AB);
                //    }                    
                    
                //    LAI = xmldoc.CreateElement("LAI");
                //    LAI.InnerText = pRow.get_Value(pRow.Fields.FindField("LAI")).ToString();
                //    xmlNode.AppendChild(LAI);
                    
                //    MCL = xmldoc.CreateElement("MCL");
                //    MCL.InnerText = pRow.get_Value(pRow.Fields.FindField("MCL")).ToString();
                        
                //    xmlNode.AppendChild(MCL);
                //}
                string strFileLocation = outFilePath;
                System.DateTime timeNow = System.DateTime.Now;
                string timeString = timeNow.Hour.ToString() + timeNow.Minute.ToString() + timeNow.Second.ToString() + timeNow.Millisecond.ToString();

                strFileLocation += timeString;
                strFileLocation += ".xml";

                xmldoc.Save(strFileLocation);

                return xmldoc;// strFileLocation;
            }
            catch (Exception ex) {


                Log.WriteException(ex.Message.ToString());
                return null;
                
                
            }
        }

    public string outputToxml(string gpsSampleShapeFileName, OracleConnection conn, int pPointCount, IFeatureWorkspace pFeatWork, string outFilePath, System.DateTime yearmonthday)
    {

        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        OracleDataReader pReader = default(OracleDataReader);

        XmlDocument xmldoc = new XmlDocument();
        XmlElement resamples = default(XmlElement);
        XmlElement pointid = default(XmlElement);
        XmlElement name = default(XmlElement);
        XmlElement lon = default(XmlElement);
        XmlElement lat = default(XmlElement);
        XmlElement xx = default(XmlElement);
        XmlElement yy = default(XmlElement);
        XmlElement xmlRSI1 = default(XmlElement);
        XmlElement xmlRSI2 = default(XmlElement);
        XmlElement xmlRSI3 = default(XmlElement);
        XmlElement xmlRSI4 = default(XmlElement);
        XmlElement xmlRSI5 = default(XmlElement);
        XmlElement xmlRSI6 = default(XmlElement);
        XmlElement xmlRSI7 = default(XmlElement);
        XmlElement xmlRSI8 = default(XmlElement);
        XmlElement xmlRSI9 = default(XmlElement);
        XmlElement xmlRSI10 = default(XmlElement);
        XmlElement xmlRSI11 = default(XmlElement);
        XmlElement xmlRSI12 = default(XmlElement);
        XmlElement xmlRSI13 = default(XmlElement);
        XmlElement xmlRSI14 = default(XmlElement);
        XmlElement xmlRSI15 = default(XmlElement);
        XmlElement DiseaseIndex = default(XmlElement);
        XmlElement CHL_AB = default(XmlElement);
        XmlElement LAI = default(XmlElement);
        XmlElement MCL = default(XmlElement);
        resamples = xmldoc.CreateElement("resamples");

        int i = 0;
        i = 0;

        IFeatureClass pFeatC = default(IFeatureClass);
        IFeatureCursor pFeatCusor = default(IFeatureCursor);
        IFeature pFeat = default(IFeature);
        pFeatC = pFeatWork.OpenFeatureClass(gpsSampleShapeFileName);

        IPoint xypoint = new Point();
        XmlElement[] point = new XmlElement[pPointCount];
        pFeatCusor = pFeatC.Search(null, false);
        pFeat = pFeatCusor.NextFeature();
        try
        {


            for (i = 0; i <= pPointCount - 1; i++)
            {

                cmd.CommandText = "select sp_disease_vi.POINTID,sp_disease_vi.POINTNAME,sp_disease_vi.SAMPDATE,sp_disease_vi.NDWI," + "sp_disease_vi.NDWI2,sp_disease_vi.NDVI,sp_disease_vi.NRI," + "sp_disease_vi.NSI,sp_disease_vi.WI,sp_disease_vi.G_SWIR,sp_disease_vi.G_SWIR2," + "sp_disease_vi.EVI,sp_disease_vi.RVI,sp_disease_vi.ARVI,sp_disease_vi.SAVI," + "sp_disease_vi.MSAVI,sp_disease_vi.DVI,sp_disease_vi.RDVI," + " " + "SP_DISEASE_PARAM.DISEASEINDEX,SP_DISEASE_PARAM.CHL_AB,SP_DISEASE_PARAM.LAI,SP_DISEASE_PARAM.MCL" + " " + "from sp_disease_vi,SP_DISEASE_PARAM" + " " + "where (sp_disease_vi.SAMPDATE = SP_DISEASE_PARAM.SAMPDATE) and" + " " + "(sp_disease_vi.POINTID = SP_DISEASE_PARAM.POINTID) and" + " " + "(sp_disease_vi.SAMPDATE=to_date('" + yearmonthday + "','yyyy-mm-dd')) and" + " " + "(sp_disease_vi.POINTID='" + pFeat.get_Value(pFeat.Fields.FindField("POINTID")).ToString() + "')";

                pReader = cmd.ExecuteReader();

                point[i] = xmldoc.CreateElement("Parcel");
                pointid = xmldoc.CreateElement("pointid");
                if (pReader.Read())
                {
                    pointid.InnerText = pReader.GetValue(0).ToString();
                }
                point[i].AppendChild(pointid);


                //lon, lat, xx, yy
                name = xmldoc.CreateElement("pointName");
                name.InnerText = pReader.GetValue(1).ToString();
                point[i].AppendChild(name);

                lon = xmldoc.CreateElement("Lon");
                lon.InnerText = pFeat.get_Value(pFeat.Fields.FindField("JIN")).ToString();
                point[i].AppendChild(lon);

                lat = xmldoc.CreateElement("Lat");
                lat.InnerText = pFeat.get_Value(pFeat.Fields.FindField("WEI")).ToString();
                point[i].AppendChild(lat);

                xx = xmldoc.CreateElement("X");
                xypoint = (IPoint)pFeat.Shape;
                xx.InnerText = xypoint.X.ToString();
                point[i].AppendChild(xx);

                yy = xmldoc.CreateElement("Y");
                xypoint = (IPoint)pFeat.Shape;
                yy.InnerText = xypoint.Y.ToString();
                point[i].AppendChild(yy);



                xmlRSI1 = xmldoc.CreateElement("NDWI");
                xmlRSI1.InnerText = pReader.GetValue(3).ToString();
                point[i].AppendChild(xmlRSI1);

                xmlRSI2 = xmldoc.CreateElement("NDWI2");
                xmlRSI2.InnerText = pReader.GetValue(4).ToString();
                point[i].AppendChild(xmlRSI2);

                xmlRSI3 = xmldoc.CreateElement("NDVI");
                xmlRSI3.InnerText = pReader.GetValue(5).ToString();
                point[i].AppendChild(xmlRSI3);

                xmlRSI4 = xmldoc.CreateElement("NRI");
                xmlRSI4.InnerText = pReader.GetValue(6).ToString();
                point[i].AppendChild(xmlRSI4);

                xmlRSI5 = xmldoc.CreateElement("NSI");
                xmlRSI5.InnerText = pReader.GetValue(7).ToString();
                point[i].AppendChild(xmlRSI5);

                xmlRSI6 = xmldoc.CreateElement("WI");
                xmlRSI6.InnerText = pReader.GetValue(8).ToString();
                point[i].AppendChild(xmlRSI6);

                xmlRSI7 = xmldoc.CreateElement("G_SWIR");
                xmlRSI7.InnerText = pReader.GetValue(9).ToString();
                point[i].AppendChild(xmlRSI7);

                xmlRSI8 = xmldoc.CreateElement("G_SWIR2");
                xmlRSI8.InnerText = pReader.GetValue(10).ToString();
                point[i].AppendChild(xmlRSI8);

                xmlRSI9 = xmldoc.CreateElement("EVI");
                xmlRSI9.InnerText = pReader.GetValue(11).ToString();
                point[i].AppendChild(xmlRSI9);

                xmlRSI10 = xmldoc.CreateElement("RVI");
                xmlRSI10.InnerText = pReader.GetValue(12).ToString();
                point[i].AppendChild(xmlRSI9);

                xmlRSI11 = xmldoc.CreateElement("ARVI");
                xmlRSI11.InnerText = pReader.GetValue(13).ToString();
                point[i].AppendChild(xmlRSI11);

                xmlRSI12 = xmldoc.CreateElement("SAVI");
                xmlRSI12.InnerText = pReader.GetValue(14).ToString();
                point[i].AppendChild(xmlRSI12);

                xmlRSI13 = xmldoc.CreateElement("MSAVI");
                xmlRSI13.InnerText = pReader.GetValue(15).ToString();
                point[i].AppendChild(xmlRSI13);

                xmlRSI14 = xmldoc.CreateElement("DVI");
                xmlRSI14.InnerText = pReader.GetValue(16).ToString();
                point[i].AppendChild(xmlRSI14);

                xmlRSI15 = xmldoc.CreateElement("RDVI");
                xmlRSI15.InnerText = pReader.GetValue(17).ToString();
                point[i].AppendChild(xmlRSI15);
                //LAI,MCL,SLFW,PLANTGDW

                DiseaseIndex = xmldoc.CreateElement("DiseaseIndex");
                DiseaseIndex.InnerText = pReader.GetValue(18).ToString();
                point[i].AppendChild(DiseaseIndex);

                CHL_AB = xmldoc.CreateElement("CHL_AB");
                CHL_AB.InnerText = pReader.GetValue(19).ToString();
                point[i].AppendChild(CHL_AB);

                LAI = xmldoc.CreateElement("LAI");
                LAI.InnerText = pReader.GetValue(20).ToString();
                point[i].AppendChild(LAI);

                MCL = xmldoc.CreateElement("MCL");
                MCL.InnerText = pReader.GetValue(21).ToString();
                point[i].AppendChild(MCL);


                pFeat = pFeatCusor.NextFeature();



                resamples.AppendChild(point[i]);
            }





            xmldoc.AppendChild(resamples);


            string strFileLocation = outFilePath;
            System.DateTime timeNow = System.DateTime.Now;
            string timeString = timeNow.Hour.ToString() + timeNow.Minute.ToString() + timeNow.Second.ToString() + timeNow.Millisecond.ToString();



            strFileLocation += timeString;
            strFileLocation += ".xml";

            xmldoc.Save(strFileLocation);




            return strFileLocation;
        }
        catch (Exception ex)
        {


            Log.WriteException(ex.Message.ToString());
            return "Error" + ex.Message.ToString();


        }


    }
















}
